package com.taxi.verificationcode.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.taxi.common.dto.ResponseResult;
import com.taxi.common.response.NumberCodeResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author ruomengjiang
 * @Date 2022/7/7
 * @Description : online-taxi
 * @Version: 1.0
 */
@RestController
public class NumberCodeController {

//    @Value("${spring.cloud.nacos.discovery.cluster-name}")
//    String clusterName;

//    @Value("${server.port}")
//    String port;

    @GetMapping("/numberCode/{size}")
    //@SentinelResource("number-code")
    public ResponseResult numberCode(@PathVariable("size") int size, HttpServletRequest request) {
        String tag = request.getHeader("tag");
        System.out.println("验证码服务收到的tag：" + tag);
        // 生成6位字母验证码
        String sourceChar = "abcdefghigklmnopqrstuvwxyz";
        char[] m = sourceChar.toCharArray();
        String resultString = "";
        for (int i = 0; i < size; i++) {
            char temp = m[new Random().nextInt(26)];
            resultString = resultString + temp;
        }
        // 定义返回值
        NumberCodeResponse response = new NumberCodeResponse();
        response.setNumberCode(resultString);
        return ResponseResult.success(response);
    }


}
